package com.its;

import com.its.provider.service.api.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author xiaxiaoyu
 * @description 测试控制器
 * @create 2023/05/25
 */
@RestController
@RequestMapping("test")
@Slf4j
public class TestController {
    @DubboReference(check = false)
    private DemoService demoService;

    @GetMapping("/sayHello")
    public String sayHello(@RequestParam String content) {
        log.info(Thread.currentThread().getName() + ", " + Thread.currentThread().getId());
        return demoService.sayHello(content);
    }

    @GetMapping("/sayHello2")
    public String sayHello2(@RequestParam String content) throws ExecutionException, InterruptedException {
        log.info(Thread.currentThread().getName() + ", " + Thread.currentThread().getId());
//        CompletableFuture<String> f = RpcContext.getContext().asyncCall(() -> demoService.sayHello(content));
        RpcContext.getContext().asyncCall(() -> {
            demoService.sayHello(content);
        });

        return "get";
    }
    
    @GetMapping("/sayHello3")
    public String sayHello3(@RequestParam String content) throws ExecutionException, InterruptedException {
        log.info("sayHello3: " + Thread.currentThread().getName() + ", " + Thread.currentThread().getId());
        CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> demoService.sayHello(content));
        future3.whenComplete((v, t) -> {
            if (t != null) {
                t.printStackTrace();
            } else {
                log.info("AsyncTask Response:" + v);
            }
        });
        return "Ok";
    }
}
